\documentclass[a4paper,11pt,fontset=none,scheme=chinese]{ctexbook}
\ctexset{
  section/format = \Large\bfseries,
  today=old
}

% **************************************
% Language & Fonts
% **************************************
\input{lang}
\usepackage{CJKnumb}
%Mapping fixes endash & emdash
\newfontfamily\lmr{Latin Modern Roman}
\newfontfamily\courier{Courier New}

%\xeCJKsetcharclass{"0}{"2E7F}{0}
%\xeCJKsetcharclass{"2E80}{"FFFF}{1}
\DeclareTextAccent{\.}{EU1}{"0307}
%\DeclareTextAccent{\v}{EU1}{"030C}
%\DeclareTextAccent{\d}{EU1}{"0323}
%\DeclareTextAccent{\b}{EU1}{"0331}

\usepackage{etex}
\usepackage{metalogo,mflogo,texnames}
\usepackage{textcomp}

\usepackage[normalem]{ulem}
%\usepackage{xltxtra}                % load etex automatically

% **************************************
% Graphics
% **************************************
\usepackage{adjustbox}
\usepackage{graphicx, rotating}
\usepackage[svgnames, table]{xcolor}% put before pstricks, textpos, tikz

\DeclareGraphicsRule{.mps}{eps}{.mps}{}
\graphicspath{{graph/}{img/}}

% **************************************
% Table
% **************************************
\usepackage{booktabs,longtable,multirow,tabularx,warpcol}

% **************************************
% Float
% **************************************
\usepackage{caption}
\DeclareCaptionType [fileext=loe]{example}[例][例目录]
%\usepackage{float}
%\newfloat{example}{tbph}{loe}
%\restylefloat*{example}
%\floatstyle{plain}
%\floatname{example}{例}
%\captionsetup[example]{position=top}
%\newcommand{\listofexamples}{\listof{example}{例目录}}
\usepackage{subfig}                 % put before fvrb-ex
\newsubfloat[position=bottom,listofformat=subsimple]{example}

% **************************************
% Structure
% **************************************
%\usepackage{xurl} % not working with url
%\PassOptionsToPackage{hyphens}{url}
\usepackage[
  bookmarksnumbered, 
  pdfencoding=auto, 
  pdfpagelayout=TwoPageRight,
  breaklinks, 
  colorlinks, 
  linkcolor=RoyalBlue, 
  urlcolor=blue]
{hyperref}
\usepackage{multicol}               % need etex
\usepackage{multind}
\makeindex{people}
\makeindex{org}
% replaced by ctexbook
% \usepackage{titlesec}

\newcommand{\lnotesauthor}{包 太 雷}
\newcommand{\lnotesversion}{v2.56}
\newcommand{\lnotesdate}{2019年4月}

% replaced by ctexbook
% \renewcommand{\contentsname}{目录}
\renewcommand{\listfigurename}{图目录}
\renewcommand{\listtablename}{表目录}
\renewcommand{\chaptername}{第\CJKnumber{\thechapter}章}
%\newcommand{\sectionname}{\thesection 节}
% \renewcommand{\figurename}{图}
% \renewcommand{\tablename}{表}
% \renewcommand{\appendixname}{附录{\thechapter}}
% \renewcommand{\bibname}{参考文献}
% \renewcommand{\indexname}{索引}

\renewcommand{\figureautorefname}{图}
\renewcommand{\tableautorefname}{表}
\renewcommand{\appendixautorefname}{附录}

% replaced by ctexbook
% \titleformat{\chapter}[block]{\center\Huge}{\chaptername}{20pt}{}
\makeatletter
\renewcommand{\tableofcontents}{%
\setlength{\columnsep}{2.5em}
%\setlength{\columnseprule}{.8pt}
\begin{multicols}{2}[\chapter*{\contentsname}]%
    \@starttoc{toc}%
\end{multicols}}
\makeatother

% **************************************
% Format
% **************************************
\usepackage{calc}
\usepackage{indentfirst,setspace}

%\usepackage{enumitem} % conflict with paralist
%\setlist[1]{labelindent=0.5\parindent,leftmargin=\parindent}
\usepackage{paralist}
\newenvironment{CompactEnum}{
\begin{enumerate}
    \setlength{\itemsep}{0pt}
    \setlength{\parsep}{0pt}
    \setlength{\parskip}{0pt}
}{\end{enumerate}}

\newenvironment{CompactItem}{
\begin{enumerate}
    \setlength{\itemsep}{0pt}
    \setlength{\parsep}{0pt}
    \setlength{\parskip}{0pt}
}{\end{enumerate}}

\newenvironment{CompactDesc}{
\begin{enumerate}
    \setlength{\itemsep}{0pt}
    \setlength{\parsep}{0pt}
    \setlength{\parskip}{0pt}
}{\end{enumerate}}

\usepackage{fancyvrb,listings,verbatim}

\VerbatimFootnotes
\usepackage{ldemo}

%\usepackage{framed}
\usepackage{marginnote}

%\renewcommand{\labelitemi}{\ensuremath{\bullet}}% xunicode changes the bullet

\makeatletter
\renewenvironment{quotation}{
    \list{}{
        \listparindent 2em
        \itemindent    \listparindent
        \rightmargin   \leftmargin
        \parsep        \z@ \@plus\p@
    }
    \item
}{
    \endlist
}
\makeatother

% **************************************
% Layout
% **************************************
\usepackage{fancyhdr}
\usepackage[absolute]{textpos}
\usepackage{varwidth}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textit{\leftmark}}
\fancyhead[LO]{\textit{\rightmark}}
\fancypagestyle{plain}{             % set header and footer of plain pages
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
    \fancyfoot[LE,RO]{\thepage}
}

\makeatletter
\renewcommand{\cleardoublepage}{
    \clearpage
    \if@twoside \ifodd 
        \c@page
    \else
        \if@openrecto 
            \thispagestyle{empty}
            \vspace*{\fill}
            \begin{center}
                广告位招租
            \end{center}
            \vspace{\fill}
            \newpage
            \if@twocolumn
                \newpage
            \fi
        \fi 
    \fi\fi
}

\newif\if@openrecto
\makeatother

% **************************************
% Bibliography
% **************************************
\usepackage{chapterbib}
\usepackage[sectionbib,super,square,sort&compress]{natbib}

% **************************************
% Math
% **************************************
\usepackage{amsmath,amsfonts,amsthm,mathrsfs}

\begin{document}
\frontmatter
\include{front}                     % Front cover and title page
\include{copyright}

% Open right pages: title, dedication, TOC
\makeatletter
\@openrectotrue
\makeatother

\include{dedication}

% TOC, LOF, LOT
\tableofcontents

% Open any pages: LOF, LOT, forward, preface, acknowledgements, abbreviations
\makeatletter
\@openrectofalse
\makeatother

\listoffigures
\listoftables
\listofexamples

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\onehalfspacing
\let\oldparskip\parskip
\addtolength{\parskip}{3pt}
\addtolength{\abovecaptionskip}{-3pt}
\addtolength{\belowcaptionskip}{-3pt}
\setlength{\parindent}{2em}

\include{forward}
\include{preface-fan}
\include{preface}
\include{preface-old}
\include{acknowledgements}

\makeatletter
\@openrectotrue
\makeatother
\mainmatter
\renewcommand{\chaptermark}[1]{\markboth{\chaptername: #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection: #1}}

\input{index}
\include{introduction}
\include{basics}
\include{fonts}
\include{math}
\include{graphics}
\include{gra-mp}
\include{gra-pst}
\include{gra-pgf}
\include{tables}
\include{structure}
\include{layout}
\include{apps}

\appendix
\renewcommand{\chaptername}{附录{\thechapter}}

\include{software}
\include{printing}

\makeatletter
\@openrectofalse
\makeatother

\backmatter

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
%\renewcommand{\chaptername}{附录{\thechapter}}

\include{postscript}
\include{postscript-old}

\singlespacing
\setlength{\parskip}{\oldparskip}

\chapter{索引}
\printindex{people}{人物索引}
\printindex{org}{组织机构索引}

\include{back}

\end{document}
